#### In-Text Results: Power ####

# Libraries
# library(tidyverse)
# library(rio)
# library(here)
# library(pwr)

# data_pnas = import(here("Data","data_pnas.rds"))

# Number of D/R in engaged sample
ndem = nrow(data_pnas[data_pnas$pid == "Democrat",])
nrep = nrow(data_pnas[data_pnas$pid == "Republican",])

# Power
p = pwr.2p2n.test(n1 = ndem,
                  n2 = nrep, 
                  sig.level = 0.05, 
                  power = 0.8, 
                  alternative = "two.sided")

cat("We are powered to detect a standard effect size of", round(p$h, 2),
    "at significance alpha =", p$sig.level,
    "with frequency", p$power, "\n")

# Power in Context
s1 = var(data_pnas$norm_judgesre[data_pnas$pid == "Republican"], na.rm = T)
s2 = var(data_pnas$norm_judgesre[data_pnas$pid == "Democrat"], na.rm = T)
s = sqrt( ((nrep - 1)*s1 + (ndem - 1)*s2) / (nrep + ndem - 2) )

cat("Standard effect size for ignoring outparty court decisions:", round(p$h*s, 3)*100, "percentage point(s) \n")